package com.sbc.user.controller;

import cn.hutool.core.util.StrUtil;
import com.sbc.convention.exception.ClientException;
import com.sbc.convention.result.Result;
import com.sbc.log.annotation.ILog;
import com.sbc.user.pojo.vo.UserInfoActualRespVo;
import com.sbc.user.pojo.vo.UserBasicInfoReqVo;
import com.sbc.user.pojo.vo.UserContactInfoReqVo;
import com.sbc.user.pojo.vo.UserInfoRespVo;
import com.sbc.user.service.UserInfoService;
import com.sbc.web.Results;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 用户信息控制层
 * @author: songbaicheng
 * @create: 2024-04-10 23:56
 **/
@ILog
@RestController
@RequestMapping("/api/user/info")
@RequiredArgsConstructor
public class UserInfoController {

    private final UserInfoService userService;

    /**
     * 根据用户名查询用户脱敏信息
     *
     * @param username 用户名
     * @return 用户信息
     */
    @GetMapping({"/query/", "/query/{username}"})
    public Result<UserInfoRespVo> queryUserByUsername(@PathVariable(value = "username", required = false) String username) {
        if (StrUtil.isEmpty(username)) {
            throw new ClientException("用户名为空！");
        }
        return Results.success(userService.queryUserInfoByUsername(username));
    }

    /**
     * 根据用户名查询用户无脱敏信息
     *
     * @param username 用户名
     * @return 用户信息
     */
    @GetMapping({"/actual/query/", "/actual/query/{username}"})
    public Result<UserInfoActualRespVo> queryActualUserByUsername(@PathVariable(value = "username", required = false) String username) {
        if (StrUtil.isEmpty(username)) {
            throw new ClientException("用户名为空！");
        }
        return Results.success(userService.queryActualUserInfoByUsername(username));
    }

    @PutMapping({"/basic/update/", "/basic/update/{username}"})
    public Result<Void> updateUserBasicInfo(@PathVariable(value = "username", required = false) String username,
                                            @Valid @RequestBody UserBasicInfoReqVo requestParam) {
        if (StrUtil.isEmpty(username)) {
            throw new ClientException("用户名为空！");
        }

        userService.updateUserBasicInfo(username, requestParam);
        return Results.success();
    }

    @PutMapping({"/contact/update/", "/contact/update/{username}"})
    public Result<Void> updateUserContactInfo(@PathVariable(value = "username", required = false) String username,
                                              @Valid @RequestBody UserContactInfoReqVo requestParam) {
        if (StrUtil.isEmpty(username)) {
            throw new ClientException("用户名为空！");
        }

        userService.updateUserContactInfo(username, requestParam);
        return Results.success();
    }
}
